package com.bw.liuyushan.liuyushan20210904.MVP.Activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.liuyushan20210904.Adapter.TableVideoAdapter;
import com.bw.liuyushan.liuyushan20210904.Adapter.VideoAdapter;
import com.bw.liuyushan.liuyushan20210904.Entity.VideoBean;
import com.bw.liuyushan.liuyushan20210904.GreenDao.VideoDate;
import com.bw.liuyushan.liuyushan20210904.GreenDao.VideoDateUtils;
import com.bw.liuyushan.liuyushan20210904.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210904.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210904.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210904.Managers.App;
import com.bw.liuyushan.liuyushan20210904.Managers.MyLayoutManager;
import com.bw.liuyushan.liuyushan20210904.Managers.MyStand;
import com.bw.liuyushan.liuyushan20210904.R;
import com.bw.liuyushan.mvp_lib.VIew.BaseActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kd.easybarrage.Barrage;
import com.kd.easybarrage.BarrageView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;

public class ItemVideoActivity extends BaseActivity<BasePresenterTest> implements IContrackTest.C_ViewTest, OnRefreshLoadMoreListener {

    private androidx.recyclerview.widget.RecyclerView rvItem;
    int currentPage = 1;
    VideoAdapter adapter;
    boolean isAdapter;
    private com.scwang.smart.refresh.layout.SmartRefreshLayout smItem;
    View inflate;
    View popu;
    ArrayList<String> arrayList;
    TextView viewById;
    private ImageView talk;
    private ImageView share;
    private com.kd.easybarrage.BarrageView barrage;
    List<VideoBean.DataBean> data;


    @Override
    public int bindLayout() {
        return R.layout.activity_itemviedo;
    }

    @Override
    public void initView() {
        //防止内存泄露
        mPresenter=new BasePresenterTest(new BaseModelTest(),this);
        rvItem = (RecyclerView) findViewById(R.id.rv_item);
        smItem = (SmartRefreshLayout) findViewById(R.id.sm_item);
        smItem.setOnRefreshLoadMoreListener(this);
//        rvItem.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        rvItem.setLayoutManager(new MyLayoutManager(this));//自定义布局管理器
        //设置滚动监听
        rvItem.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE:
                        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                        MyStand viewById = layoutManager.getChildAt(0).findViewById(R.id.stand);
                        viewById.startPlayLogic();
                        break;
                }
            }
        });

        popu = LayoutInflater.from(this).inflate(R.layout.popupindow, null);
        inflate = LayoutInflater.from(this).inflate(R.layout.activity_itemviedo, null);

        talk = (ImageView) findViewById(R.id.talk);
        share = (ImageView) findViewById(R.id.share);

        barrage = (BarrageView) findViewById(R.id.barrage);
    }

    @Override
    public void initDate() {
        mPresenter.mpVideo("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);

        arrayList = new ArrayList<>();

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                UMImage image =new UMImage(ItemVideoActivity.this, R.drawable.wadm);//资源文件
                UMWeb web =new UMWeb(data.get(0).getVideopath());
                new ShareAction(ItemVideoActivity.this).withMedia(web).withText("视频地址链接").setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                        .setCallback(new UMShareListener() {
                            @Override
                            public void onStart(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onResult(SHARE_MEDIA share_media) {

                            }

                            @Override
                            public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                            }

                            @Override
                            public void onCancel(SHARE_MEDIA share_media) {

                            }
                        }).open();
            }
        });
        talk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupWindow popupWindow = new PopupWindow(getApplicationContext());
                popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                popupWindow.setHeight(300);
                popupWindow.setContentView(popu);
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);
                popupWindow.showAtLocation(v, Gravity.BOTTOM,300,0);
            }
        });
        popu.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewById = popu.findViewById(R.id.talkin);
                String string = viewById.getText().toString();
                arrayList.add(string);
                viewById.setText("");//清空输入框
                barrage.addBarrage(new Barrage(string));
            }
        });


    }

    @Override
    public void ShowVideo(VideoBean videoBean) {

        data = videoBean.getData();
        initAdapter(data);
    }
    private void initAdapter(List<VideoBean.DataBean> data) {
        smItem.finishRefresh();
        smItem.finishLoadMore();
        if (adapter==null){
            adapter=new VideoAdapter(data);
            rvItem.setAdapter(adapter);
            adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                    VideoDate videoDate = new VideoDate();
                    videoDate.setTitle(data.get(position).getCaption());
                    videoDate.setPath(data.get(position).getVideopath());
                    VideoDateUtils.getInstance().getDaoSession().getVideoDateDao().insert(videoDate);
                    ToastUtils.showShort("下载成功,已存入数据库");
                }
            });
        }else{
            if (isAdapter){
                adapter.getData().clear();
            }
            adapter.getData().addAll(data);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isAdapter=false;
        currentPage++;
        mPresenter.mpVideo("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isAdapter=true;
        currentPage=1;
        mPresenter.mpVideo("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
